#include "public.h"
#include "smg.h"
#include "ds18b20.h"
void smg_display(u8 dat[],u8 pos)
{
u8 i=0;
u8 pos_temp=pos-1;
for(i=pos_temp;i<8;i++)
{
switch(i)//??
{
case 0: LSC=1;LSB=1;LSA=1;break;
case 1: LSC=1;LSB=1;LSA=0;break;
case 2: LSC=1;LSB=0;LSA=1;break;
case 3: LSC=1;LSB=0;LSA=0;break;
	case 4: LSC=0;LSB=1;LSA=1;break;
case 5: LSC=0;LSB=1;LSA=0;break;
case 6: LSC=0;LSB=0;LSA=1;break;
case 7: LSC=0;LSB=0;LSA=0;break;
}
SMG_A_DP_PORT=dat[i-pos_temp];//??????
delay_10us(100);//??????,??????
SMG_A_DP_PORT=0x00;//??
}
}
u8 ds18b20_init(void)
{
ds18b20_reset();
return ds18b20_check();
}
void ds18b20_reset(void)
{
DS18B20_PORT=0; //?? DQ
delay_10us(75); //?? 750us
DS18B20_PORT=1; //DQ=1
delay_10us(2); //20US
}
/********************************************************************
***********
* ? ? ? : ds18b20_check
* ???? : ?? DS18B20 ????
* ? ? : ?
* ? ? : 1:???? DS18B20 ???,0:??
*********************************************************************
**********/
u8 ds18b20_check(void)
{
u8 time_temp=0;
while(DS18B20_PORT&&time_temp<20) //?? DQ ????
{
time_temp++;
delay_10us(1);
}
if(time_temp>=20)return 1; //????????? 1
else time_temp=0;
while((!DS18B20_PORT)&&time_temp<20) //?? DQ ????
{
time_temp++;
delay_10us(1);
}
if(time_temp>=20)return 1; //????????? 1
return 0;
}
float ds18b20_read_temperture(void)
{
float temp;
u8 dath=0;
u8 datl=0;
u16 value=0;
ds18b20_start();//????
ds18b20_reset();//??
ds18b20_check();
ds18b20_write_byte(0xcc);//SKIP ROM
ds18b20_write_byte(0xbe);//????
datl=ds18b20_read_byte();//???
dath=ds18b20_read_byte();//???
value=(dath<<8)+datl;//??? 16 ???
if((value&0xf800)==0xf800)//?????,???
{
value=(~value)+1; //?????? 1
temp=value*(-0.0625);//????
}
else //???
{
temp=value*0.0625;
}
return temp;
}
void ds18b20_start(void)
{
ds18b20_reset();//??
ds18b20_check();//?? DS18B20
ds18b20_write_byte(0xcc);//SKIP ROM
ds18b20_write_byte(0x44);//????
}
void main()
{
u8 i=0;
int temp_value;
u8 temp_buf[5];
ds18b20_init();//??? DS18B20
while(1)
{
i++;
if(i%50==0)//???????????,??????????????????
temp_value=ds18b20_read_temperture()*10;//??????????
if(temp_value<0)//???
{
temp_value=-temp_value;
temp_buf[0]=0x40;//????
}
else
temp_buf[0]=0x00;//???
temp_buf[1]=gsmg_code[temp_value/1000];//??
temp_buf[2]=gsmg_code[temp_value%1000/100];//??
temp_buf[3]=gsmg_code[temp_value%1000%100/10]|0x80;//??+??
?
temp_buf[4]=gsmg_code[temp_value%1000%100%10];//??????
smg_display(temp_buf,4);
}
}